package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 17:35
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @RequestMapping("/test")
    public String test1(){
        return "这个search微服务已经被调用了！";
    }

    @RequestMapping("/query/{id}")
    //指定兜底方法的方法名，如果这个方法出现异常，那么会自动执行兜底方法返回结果
    @HystrixCommand(fallbackMethod = "findUserByIdFallBack")
    public User findUserById(@PathVariable("id") Integer id){
        System.out.println("=========="+id);
        //模拟被调用方出错
        int i = 1/0;
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setBirthday(new Date());
        return user;
    }

    /**
     * 兜底方法，在被调用方法处理异常
     * @param id
     * @return
     */
    public User findUserByIdFallBack(Integer id){
        User user = new User();
        user.setId(1);
        user.setName("出错啦！");
        return user;
    }
}
